package com.adv.netWork02.loginServer;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 1:服务器上存有多个用户对象（包括用户和密码），客户端通过socket登录，如果成功，输出欢迎信息，失败给出提示。
 * 
 * @author zhaoyang
 *
 */
public class LoginClient {

	public static void main(String[] args) throws Exception {
		Scanner input = new Scanner(System.in);
		Socket s = new Socket("localhost", 9999);
		System.out.println("-----------客户端-------------");
		// 读入
		InputStream in = s.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		// 写出
		OutputStream os = s.getOutputStream();
		ObjectOutputStream oout = new ObjectOutputStream(os);
		while (true) {
			Map<String, String> map = new HashMap<String, String>();
			// System.out.println("链接服务器成功！");
			String reply = "";
			reply = br.readLine();
			System.out.println("服务器说:" + reply);

			System.out.print("请输入用户名:");
			String name = input.next();
			System.out.print("请输入密码:");
			String password = input.next();
			map.put(name, password);
			oout.writeObject(map);
			reply = br.readLine();
			System.out.println("服务器说:" + reply);
			if (reply.equals("登录成功，欢迎进入系统！")) {
				break;
			}
		}
		s.close();
		System.out.println("客户端关闭！");

	}

}
